package io.zw.modules.xinpai.controller;


import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.zw.common.validator.ValidatorUtils;
import io.zw.common.validator.group.AddGroup;
import io.zw.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.web.bind.annotation.*;
import io.zw.modules.xinpai.entity.UsersEntity;
import io.zw.modules.xinpai.service.UsersService;
import io.zw.common.utils.PageUtils;
import io.zw.common.utils.R;



/**
 * 
 *
 * @author zhuwei
 * @email 121203654@qq.com
 * @date 2018-10-10 13:15:04
 */
@RestController
@RequestMapping("xinpai/users")
public class UsersController  extends AbstractController {
    @Autowired
    private UsersService usersService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("xinpai:users:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("firm_id",getDeptId());
        PageUtils page = usersService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("xinpai:users:list")
    public R select(@RequestParam Map<String, String> params){
        List<UsersEntity> list=usersService.selectList(new EntityWrapper<UsersEntity>().eq("firm_id",getDeptId())
                .like(StringUtils.isNotBlank(params.get("name")),"position_id",params.get("name"), SqlLike.DEFAULT)
                .eq(StringUtils.isNotBlank(params.get("state")),"state",params.get("state"))
                .eq(StringUtils.isNotBlank(params.get("projectId")),"project_id",params.get("projectId"))
        );
        if(params.get("nexus")!=null&&params.get("nexus").equals("true")){
            for (int i = 0; i < list.size(); i++) {
                UsersEntity usersEntity =  list.get(i);
                usersEntity.setAreaList(usersService.queryAreaList(usersEntity.getUId()));
            }
        }
        return R.ok().put("list",list);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{uId}")
    @RequiresPermissions("xinpai:users:info")
    public R info(@PathVariable("uId") Integer uId){
        UsersEntity users = usersService.selectOne(new EntityWrapper<UsersEntity>().eq("u_id",uId).eq("firm_id",getDeptId()));
        if(users!=null){
            //获取用户所属的区域列表
            List<Long> arorList=usersService.queryAreaList(users.getUId());
            users.setAreaList(arorList);
            users.setFirmId(getDeptId());
            return R.ok().put("users", users);
        }else{
            return R.error("非法查询");
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("xinpai:users:save")
    public R save(@RequestBody UsersEntity users){
        ValidatorUtils.validateEntity(users, AddGroup.class);
        users.setState(0);
        users.setFirmId(getDeptId());
        usersService.save(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("xinpai:users:update")
    public R update(@RequestBody UsersEntity users){
        ValidatorUtils.validateEntity(users, AddGroup.class);
        usersService.update(users);//全部更新

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("xinpai:users:delete")
    public R delete(@RequestBody Integer[] uIds){
        usersService.deleteBatchIds(Arrays.asList(uIds));

        return R.ok();
    }
}
